package com.xoa.service.attend;


import com.xoa.dao.attend.AttendMachineMapper;
import com.xoa.model.attend.AttendMachine;
import com.xoa.util.ToJson;
import com.xoa.util.common.L;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 创建作者:   牛江丽
 * 创建日期:   ${date} ${time}
 * 类介绍  :
 * 构造参数:
 */
@Service
public class AttendMachineService {
    @Autowired
    private AttendMachineMapper attendMachineMapper;

    /**
     * 创建作者:   牛江丽
     * 创建日期:   2018年04月12日 下午10:15:00
     * 方法介绍:   修改考勤机
     * 参数说明:   @param autoId
     * 返回值说明:
     */
    @Transactional
    public ToJson<AttendMachine> updateMachine(AttendMachine attendMachine){
        ToJson<AttendMachine> json = new ToJson<AttendMachine>(1, "error");
        try{
            List<AttendMachine> list=attendMachineMapper.selectMachine(attendMachine);
            int count=0;
            if(list!=null && list.size()>0){
                attendMachine.setMachineid(list.get(0).getMachineid());
                count+=attendMachineMapper.updateByPrimaryKeySelective(attendMachine);
            }else{
                count+=attendMachineMapper.insertSelective(attendMachine);
            }
            if(count>0){
                json.setMsg("ok");
                json.setFlag(0);
            }
        }catch(Exception e){
            json.setMsg(e.getMessage());
            L.e("AttendMachineService updateMachine:"+e);
            e.printStackTrace();
        }
        return  json;
    }

    /**
     * 创建作者:   牛江丽
     * 创建日期:   2018年04月12日 下午10:15:00
     * 方法介绍:   查询考勤机
     * 参数说明:   @param autoId
     * 返回值说明:
     */
    public ToJson<AttendMachine> selMachine(AttendMachine attendMachine){
        ToJson<AttendMachine> json = new ToJson<AttendMachine>(1, "error");
        try{
            List<AttendMachine> list=attendMachineMapper.selectMachine(attendMachine);
            if(list!=null && list.size()>0){
                json.setObject(list.get(0));
            }
            json.setMsg("ok");
            json.setFlag(0);
        }catch(Exception e){
            json.setMsg(e.getMessage());
            L.e("AttendMachineService selMachine:"+e);
            e.printStackTrace();
        }
        return  json;
    }

    public void connectAttendMachine(){
        AttendMachine attendMachine=new AttendMachine();
        List<AttendMachine> list=attendMachineMapper.selectMachine(new AttendMachine());
        if(list!=null && list.size()>0){
            attendMachine=list.get(0);
        }
        if("mysql".equals(attendMachine.getDatabaseType())){

        }
    }

}
